﻿using AutoMapper;
using FurnitureDesign.Application.Products;
using FurnitureDesign.Application.Products.Dtos;
using FurnitureDesign.Core.Models;
using FurnitureDesign.Core.Repository;
using FurnitureDesign.Mvc.Controllers;
using FurnitureDesign.Mvc.Models.Products;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FurnitureDesign.Mvc.Models;

namespace FurnitureDesign.Mvc.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class ProductController : BaseController
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IRepository<Product, int> _repository;
        private readonly IProductService _productService;
        private readonly IMapper _mapper;
        private readonly IRepository<ProductType, int> _productRepository;

        public ProductController(ILogger<HomeController> logger, IRepository<Product, int> repository,
           IProductService  productService, IMapper mapper, IRepository<ProductType, int> productRepository)
        {
            _logger = logger;
            _repository = repository;
            _productService = productService;
            _mapper = mapper;
            _productRepository = productRepository;
        }
        public async Task<ActionResult> Index()
        {
            ViewBag.ProductList = new MultiSelectList(await _productRepository.GetAllListAsync(), "Id", "ProductTypeName");
            return View();
        }
        public async Task<ActionResult> List(ProductInputDto input)
        {
            var stus = await _productService.GetPagedResult(input);
            return Json(ListResult(_mapper.Map<List<ProductViewModel>>(stus.Data), stus.TotalCount));
        }
        public async Task<IActionResult> GetInsert()
        {
            ViewBag.ProductList = new MultiSelectList(await _productRepository.GetAllListAsync(), "Id", "ProductTypeName");
            return View();
        }
        public async Task<ActionResult> Insert(ProductViewModel productViewModel)
        {
            Product product=await _repository.InsertAsync(_mapper.Map<Product>(productViewModel));
            if (product == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
        }
        public async Task<IActionResult> GetEdit(int id)
        {
            Product pro = await _repository.FirstOrDefaultAsync(s => s.Id == id);
            ViewBag.ProductList = new MultiSelectList(await _productRepository.GetAllListAsync(), "Id", "ProductTypeName");
            return View(_mapper.Map<ProductViewModel>(pro));
        }
        public async Task<ActionResult> Edit(ProductViewModel productViewModel)
        {
            Product product = await _repository.UpdateAsync(_mapper.Map<Product>(productViewModel));
            if (product == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
        }
        public async Task<ActionResult> Delete(int id)
        {
            await _repository.DeleteAsync(s => s.Id == id);
            return Json(AjaxResult(true));
        }
    }
}
